import { DBObject } from './DBObject';
import { Product } from './Product';
import { User } from './User';
import moment from 'moment';
export const PRESALE_TIME_SPAN = 3;
export const PRESALE_TIME_SPAN_UNIT = 'year';
export enum OrderState {
    invalid = 0,
    created = 1,
    payed = 2,
    started = 3,
    outdate = 4,
    finished = 5
}
export enum PayType {
    cash = "现金",
    card = "刷卡",
    alipay = "支付宝",
    wechat = "微信",
    other = '其他'
}
export class Order extends DBObject {
    public buyerName?: string = '';
    public productTitle?: string = '';
    public payDate?: number;
    public startDate?: number;
    public outDate?: number;
    public finishDate?: number;
    public invalidDate?: number;
    public state: OrderState = OrderState.created;
    public stateString: string = '';
    public isPayed?: boolean;
    public remarks?: string;
    public user?: User;
    public product?: Product;
    public payType?: PayType | string;
    public reference: string[] = [];
    public isPresale: boolean = false;
    public presaleOutdate?: number;
    public presaleNoticeList: boolean = false;
    constructor(order: any) {
        super(8);
        Object.assign(this, order);
        if (order.user) {
            let u = new User(order.user);
            this.setUser(u);
        }
        if (order.product) {
            let p = new Product(order.product);
            this.setProduct(p);
        }
        if (this.state === OrderState.started) {
            let endMoment = moment(this.outDate);
            if (moment().isAfter(endMoment)) {
                this.state = OrderState.outdate;
            }
        }
        this.updateStateString();
        this.checkPresale();
    }
    public isNoticeOrder() {
        let result = false;
        if (this.state !== OrderState.started) {
            return false;
        }
        if (this.outDate) {
            let endDateMoment = moment(this.outDate);
            let days = endDateMoment.diff(moment(), "days");
            if (days >= 0 && days <= 7) {
                result = true;
            }
        }
        return result;
    }
    public setUser(user: User) {
        this.user = user;
        this.buyerName = user.buyerName;
        this.update();
    }
    public setProduct(product: Product) {
        this.product = product;
        this.productTitle = product.title;
        this.update();
    }
    public setPresaleState(state: boolean = false) {
        if (this.isPresale === state) {
            return;
        }
        this.isPresale = state;
        if (this.isPresale) {
            this.presaleOutdate = moment().add(PRESALE_TIME_SPAN, PRESALE_TIME_SPAN_UNIT).valueOf();
        } else {
            this.presaleOutdate = undefined;
            this.presaleNoticeList = false;
        }
        this.checkPresale();
    }
    public checkPresale() {
        if (this.isPresale) {
            let diff = moment().diff(moment(this.presaleOutdate), 'months');
            if (diff === 0) {
                this.presaleNoticeList = true;
            } else {
                this.presaleNoticeList = false;
            }
        }
    }
    public pay(type: string) {
        this.state = OrderState.payed;
        this.payType = type;
        this.payDate = moment().valueOf();
        this.isPayed = true;
        this.setPresaleState(true);
        this.updateStateString();
        this.update();
    }
    public startCount() {
        if (this.product) {
            this.state = OrderState.started;
            this.updateStateString();
            this.setPresaleState(false);
            this.update();
            this.startDate = moment().valueOf();
            this.outDate = moment(this.startDate).add(this.product.duration, this.product.durationUnit).valueOf();
        }
    }
    public invalid() {
        this.state = OrderState.invalid;
        this.invalidDate = moment().valueOf();
        this.update();
        this.updateStateString();
    }
    public attachImages(images: string[]) {
        this.reference = images;
    }
    public finish() {
        this.state = OrderState.finished;
        this.finishDate = moment().valueOf();
        this.update();
        this.updateStateString();
    }
    private updateStateString() {
        switch (this.state) {
            case OrderState.invalid:
                this.stateString = this.isPayed ? '作废（已付款）' : '作废';
                break;
            case OrderState.created:
                this.stateString = '未支付';
                break;
            case OrderState.payed:
                this.stateString = '已支付';
                break;
            case OrderState.started:
                this.stateString = '进行中';
                break;
            case OrderState.outdate:
                this.stateString = '已到期';
                break;
            case OrderState.finished:
                this.stateString = '已完成';
                break;
        }
    }
}